package common

import (
	"errors"
	"fmt"
	"runtime/debug"
	"strings"
)

// ErrPanic 判断是否error(完全匹配或模糊匹配)，否则panic
func ErrPanic(err error, ignoreErrs ...error) {
	if err == nil {
		return
	}
	for _, e := range ignoreErrs {
		if errors.Is(err, e) {
			return
		}
		if strings.Index(err.Error(), e.Error()) >= 0 {
			return
		}
	}
	panic(err)
}

// CatchPanic 捕获异常处理
func CatchPanic(fs ...func(interface{})) {
	if err := recover(); err != nil {
		fmt.Printf("recover panic %s\n", err)
		fmt.Printf("recover stack: %s\n", debug.Stack())
		for _, f := range fs {
			f(err)
		}
	}
}
